package com.sy.core.base.service;
//

import com.baomidou.framework.service.impl.CommonServiceImpl;
import com.sy.core.base.common.Page;
import com.sy.core.base.model.BaseIMapper;
import com.sy.core.base.model.SimpleModel;
import com.sy.core.exception.BaseException;
import com.sy.core.exception.BusinessException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 业务逻辑层基类
 *
 * @param <M>
 * @param <T>
 */
public abstract class BaseServiceImpl<M extends BaseIMapper<T>, T extends SimpleModel> extends CommonServiceImpl<M, T> implements IBaseService<T> {

    public Logger logger = LoggerFactory.getLogger(BaseServiceImpl.class);

    /**
     * 通过map查找集合
     *
     * @param m
     * @return
     */
    public List<T> findListByMap(Map<String, Object> m) throws BaseException {
        return this.findListByMapSimple(m);
    }

    /**
     * 通用分页方法
     *
     * @param page
     * @param m
     * @return
     */
    public Page<?> getPage(Page<?> page, Map<String, Object> m) throws BaseException {
        return this.getPageSimple(page, m);
    }

    /***
     * 以下方法为了兼容旧ERP所增加的方法
     */

    public List<T> getList() throws BaseException {
        return this.getListSimple();
    }


    public void add(T object) throws BaseException {
        this.addSimple(object);
    }

    public T edit(T object) throws BaseException {
        return this.editSimple(object);
    }

    public T get(String id) throws BaseException {
        return this.getSimple(id);
    }

    public int remove(String id) throws BaseException {
        return this.removeSimple(id);
    }

    public int removeByIdArr(String[] idArr) throws BaseException {
        return this.removeByIdArrSimple(idArr);
    }

    public int removeByIds(String ids) throws BaseException {
        return this.removeByIdsSimple(ids);
    }

    public int selectCount(Map m) {
        return this.baseMapper.selectCount(m);
    }

    public Page<?> getPageSimple(Page<?> page, Map<String, Object> m) throws BaseException {
        int pageNum = page.getPage();
        int pageSize = page.getPageSize();
        int startNum = (pageNum - 1) * pageSize;
        m.put("startNum", startNum);
        m.put("pageSize", pageSize);
        m.put("dir", page.getDir());
        m.put("sort", page.getSort());
        List list = this.findListByMap(m);
        int count = this.baseMapper.selectCount(m);
        page.setCollection(list);
        page.setTotalSize(count);
        return page;
    }

    public List<T> findListByMapSimple(Map<String, Object> m) throws BaseException {
        return this.baseMapper.findListByMap(m);

    }

    public List<T> getListSimple() throws BaseException {
        return this.baseMapper.findListByMap(new HashMap());
    }

    public void addSimple(T object) throws BaseException {
        object.setId(null);
        this.baseMapper.insert(object);
    }

    public void addNotSimple(T object) throws BaseException {
        this.baseMapper.insert(object);
    }

    public T editSimple(T object) throws BaseException {
        this.baseMapper.updateSelectiveById(object);
        return object;
    }

    public T getSimple(String id) throws BaseException {
        return this.baseMapper.get(id);
    }


    public int removeSimple(String id) throws BaseException {
        return this.baseMapper.deleteById(id);
    }

    public int removeByIdArrSimple(String[] idArr) throws BaseException {
        List<String> list = java.util.Arrays.asList(idArr);
        return this.baseMapper.deleteBatchIds(list);
    }

    public int removeByIdsSimple(String ids) throws BaseException {
        String[] arr = ids.split(",");
        List<String> list = java.util.Arrays.asList(arr);
        return this.baseMapper.deleteBatchIds(list);
    }

    public T updateNotNull(T object) throws BaseException {
        this.baseMapper.updateSelectiveById(object);
        return object;
    }

    public boolean checkModify(T object) throws BaseException {
        return true;
    }


}
